home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python151_Src.lha / Python1.5_Source / Python / marshal.c < prev    next >
C/C++ Source or Header  |  1998-05-30  |  15KB  |  754 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Write Python objects to files and read them back.
  33.    This is intended for writing and reading compiled Python code only;
  34.    a true persistent storage facility would be much harder, since
  35.    it would have to take circular links and sharing into account. */
  36.  
  37. #include "Python.h"
  38. #include "longintrepr.h"
  39. #include "compile.h"
  40. #include "marshal.h"
  41.  
  42. #define TYPE_NULL    '0'
  43. #define TYPE_NONE    'N'
  44. #define TYPE_ELLIPSIS   '.'
  45. #define TYPE_INT    'i'
  46. #define TYPE_INT64    'I'
  47. #define TYPE_FLOAT    'f'
  48. #define TYPE_COMPLEX    'x'
  49. #define TYPE_LONG    'l'
  50. #define TYPE_STRING    's'
  51. #define TYPE_TUPLE    '('
  52. #define TYPE_LIST    '['
  53. #define TYPE_DICT    '{'
  54. #define TYPE_CODE    'c'
  55. #define TYPE_UNKNOWN    '?'
  56.  
  57. typedef struct {
  58.     FILE *fp;
  59.     int error;
  60.     /* If fp == NULL, the following are valid: */
  61.     PyObject *str;
  62.     char *ptr;
  63.     char *end;
  64. } WFILE;
  65.  
  66. #include "protos/marshal_protos.h"
  67.  
  68. #define w_byte(c, p) if (((p)->fp)) putc((c), (p)->fp); \
  69.               else if ((p)->ptr != (p)->end) *(p)->ptr++ = (c); \
  70.                else w_more(c, p)
  71.  
  72. static void
  73. w_more(c, p)
  74.     char c;
  75.     WFILE *p;
  76. {
  77.     int size, newsize;
  78.     if (p->str == NULL)
  79.         return; /* An error already occurred */
  80.     size = PyString_Size(p->str);
  81.     newsize = size + 1024;
  82.     if (_PyString_Resize(&p->str, newsize) != 0) {
  83.         p->ptr = p->end = NULL;
  84.     }
  85.     else {
  86.         p->ptr = PyString_AS_STRING((PyStringObject *)p->str) + size;
  87.         p->end =
  88.             PyString_AS_STRING((PyStringObject *)p->str) + newsize;
  89.         *p->ptr++ = c;
  90.     }
  91. }
  92.  
  93. static void
  94. w_string(s, n, p)
  95.     char *s;
  96.     int n;
  97.     WFILE *p;
  98. {
  99.     if (p->fp != NULL) {
  100.         fwrite(s, 1, n, p->fp);
  101.     }
  102.     else {
  103.         while (--n >= 0) {
  104.             w_byte(*s, p);
  105.             s++;
  106.         }
  107.     }
  108. }
  109.  
  110. static void
  111. w_short(x, p)
  112.     int x;
  113.     WFILE *p;
  114. {
  115.     w_byte( x      & 0xff, p);
  116.     w_byte((x>> 8) & 0xff, p);
  117. }
  118.  
  119. static void
  120. w_long(x, p)
  121.     long x;
  122.     WFILE *p;
  123. {
  124.     w_byte((int)( x      & 0xff), p);
  125.     w_byte((int)((x>> 8) & 0xff), p);
  126.     w_byte((int)((x>>16) & 0xff), p);
  127.     w_byte((int)((x>>24) & 0xff), p);
  128. }
  129.  
  130. #if SIZEOF_LONG > 4
  131. static void
  132. w_long64(x, p)
  133.     long x;
  134.     WFILE *p;
  135. {
  136.     w_long(x, p);
  137.     w_long(x>>32, p);
  138. }
  139. #endif
  140.  
  141. static void
  142. w_object(v, p)
  143.     PyObject *v;
  144.     WFILE *p;
  145. {
  146.     int i, n;
  147.     
  148.     if (v == NULL) {
  149.         w_byte(TYPE_NULL, p);
  150.     }
  151.     else if (v == Py_None) {
  152.         w_byte(TYPE_NONE, p);
  153.     }
  154.     else if (v == Py_Ellipsis) {
  155.             w_byte(TYPE_ELLIPSIS, p);
  156.     }
  157.     else if (PyInt_Check(v)) {
  158.         long x = PyInt_AS_LONG((PyIntObject *)v);
  159. #if SIZEOF_LONG > 4
  160.         long y = x>>31;
  161.         if (y && y != -1) {
  162.             w_byte(TYPE_INT64, p);
  163.             w_long64(x, p);
  164.         }
  165.         else
  166. #endif
  167.             {
  168.             w_byte(TYPE_INT, p);
  169.             w_long(x, p);
  170.         }
  171.     }
  172.     else if (PyLong_Check(v)) {
  173.         PyLongObject *ob = (PyLongObject *)v;
  174.         w_byte(TYPE_LONG, p);
  175.         n = ob->ob_size;
  176.         w_long((long)n, p);
  177.         if (n < 0)
  178.             n = -n;
  179.         for (i = 0; i < n; i++)
  180.             w_short(ob->ob_digit[i], p);
  181.     }
  182.     else if (PyFloat_Check(v)) {
  183.         extern void PyFloat_AsString
  184.             Py_PROTO((char *, PyFloatObject *));
  185.         char buf[256]; /* Plenty to format any double */
  186.         PyFloat_AsString(buf, (PyFloatObject *)v);
  187.         n = strlen(buf);
  188.         w_byte(TYPE_FLOAT, p);
  189.         w_byte(n, p);
  190.         w_string(buf, n, p);
  191.     }
  192. #ifndef WITHOUT_COMPLEX
  193.     else if (PyComplex_Check(v)) {
  194.         extern void PyFloat_AsString
  195.             Py_PROTO((char *, PyFloatObject *));
  196.         char buf[256]; /* Plenty to format any double */
  197.         PyFloatObject *temp;
  198.         w_byte(TYPE_COMPLEX, p);
  199.         temp = (PyFloatObject*)PyFloat_FromDouble(
  200.             PyComplex_RealAsDouble(v));
  201.         PyFloat_AsString(buf, temp);
  202.         Py_DECREF(temp);
  203.         n = strlen(buf);
  204.         w_byte(n, p);
  205.         w_string(buf, n, p);
  206.         temp = (PyFloatObject*)PyFloat_FromDouble(
  207.             PyComplex_ImagAsDouble(v));
  208.         PyFloat_AsString(buf, temp);
  209.         Py_DECREF(temp);
  210.         n = strlen(buf);
  211.         w_byte(n, p);
  212.         w_string(buf, n, p);
  213.     }
  214. #endif
  215.     else if (PyString_Check(v)) {
  216.         w_byte(TYPE_STRING, p);
  217.         n = PyString_Size(v);
  218.         w_long((long)n, p);
  219.         w_string(PyString_AsString(v), n, p);
  220.     }
  221.     else if (PyTuple_Check(v)) {
  222.         w_byte(TYPE_TUPLE, p);
  223.         n = PyTuple_Size(v);
  224.         w_long((long)n, p);
  225.         for (i = 0; i < n; i++) {
  226.             w_object(PyTuple_GET_ITEM(v, i), p);
  227.         }
  228.     }
  229.     else if (PyList_Check(v)) {
  230.         w_byte(TYPE_LIST, p);
  231.         n = PyList_Size(v);
  232.         w_long((long)n, p);
  233.         for (i = 0; i < n; i++) {
  234.             w_object(PyList_GetItem(v, i), p);
  235.         }
  236.     }
  237.     else if (PyDict_Check(v)) {
  238.         int pos;
  239.         PyObject *key, *value;
  240.         w_byte(TYPE_DICT, p);
  241.         /* This one is NULL object terminated! */
  242.         pos = 0;
  243.         while (PyDict_Next(v, &pos, &key, &value)) {
  244.             w_object(key, p);
  245.             w_object(value, p);
  246.         }
  247.         w_object((PyObject *)NULL, p);
  248.     }
  249.     else if (PyCode_Check(v)) {
  250.         PyCodeObject *co = (PyCodeObject *)v;
  251.         w_byte(TYPE_CODE, p);
  252.         w_short(co->co_argcount, p);
  253.         w_short(co->co_nlocals, p);
  254.         w_short(co->co_stacksize, p);
  255.         w_short(co->co_flags, p);
  256.         w_object((PyObject *)co->co_code, p);
  257.         w_object(co->co_consts, p);
  258.         w_object(co->co_names, p);
  259.         w_object(co->co_varnames, p);
  260.         w_object(co->co_filename, p);
  261.         w_object(co->co_name, p);
  262.         w_short(co->co_firstlineno, p);
  263.         w_object(co->co_lnotab, p);
  264.     }
  265.     else {
  266.         w_byte(TYPE_UNKNOWN, p);
  267.         p->error = 1;
  268.     }
  269. }
  270.  
  271. void
  272. PyMarshal_WriteLongToFile(x, fp)
  273.     long x;
  274.     FILE *fp;
  275. {
  276.     WFILE wf;
  277.     wf.fp = fp;
  278.     wf.error = 0;
  279.     w_long(x, &wf);
  280. }
  281.  
  282. void
  283. PyMarshal_WriteObjectToFile(x, fp)
  284.     PyObject *x;
  285.     FILE *fp;
  286. {
  287.     WFILE wf;
  288.     wf.fp = fp;
  289.     wf.error = 0;
  290.     w_object(x, &wf);
  291. }
  292.  
  293. typedef WFILE RFILE; /* Same struct with different invariants */
  294.  
  295. #include "protos/marshal_protos2.h"
  296.  
  297. #define rs_byte(p) (((p)->ptr != (p)->end) ? (unsigned char)*(p)->ptr++ : EOF)
  298.  
  299. #define r_byte(p) ((p)->fp ? getc((p)->fp) : rs_byte(p))
  300.  
  301. static int
  302. r_string(s, n, p)
  303.     char *s;
  304.     int n;
  305.     RFILE *p;
  306. {
  307.     if (p->fp != NULL)
  308.         return fread(s, 1, n, p->fp);
  309.     if (p->end - p->ptr < n)
  310.         n = p->end - p->ptr;
  311.     memcpy(s, p->ptr, n);
  312.     p->ptr += n;
  313.     return n;
  314. }
  315.  
  316. static int
  317. r_short(p)
  318.     RFILE *p;
  319. {
  320.     register short x;
  321.     x = r_byte(p);
  322.     x |= r_byte(p) << 8;
  323.     /* XXX If your short is > 16 bits, add sign-extension here!!! */
  324.     return x;
  325. }
  326.  
  327. static long
  328. r_long(p)
  329.     RFILE *p;
  330. {
  331.     register long x;
  332.     register FILE *fp = p->fp;
  333.     if (fp) {
  334.         x = getc(fp);
  335.         x |= (long)getc(fp) << 8;
  336.         x |= (long)getc(fp) << 16;
  337.         x |= (long)getc(fp) << 24;
  338.     }
  339.     else {
  340.         x = rs_byte(p);
  341.         x |= (long)rs_byte(p) << 8;
  342.         x |= (long)rs_byte(p) << 16;
  343.         x |= (long)rs_byte(p) << 24;
  344.     }
  345. #if SIZEOF_LONG > 4
  346.     /* Sign extension for 64-bit machines */
  347.     x <<= (8*sizeof(long) - 32);
  348.     x >>= (8*sizeof(long) - 32);
  349. #endif
  350.     return x;
  351. }
  352.  
  353. static long
  354. r_long64(p)
  355.     RFILE *p;
  356. {
  357.     register long x;
  358.     x = r_long(p);
  359. #if SIZEOF_LONG > 4
  360.     x = (x & 0xFFFFFFFF) | (r_long(p) << 32);
  361. #else
  362.     if (r_long(p) != 0) {
  363.         PyObject *f = PySys_GetObject("stderr");
  364.         if (f != NULL)
  365.             (void) PyFile_WriteString(
  366.                 "Warning: un-marshal 64-bit int in 32-bit mode\n",
  367.                 f);
  368.         PyErr_Clear();
  369.     }
  370. #endif
  371.     return x;
  372. }
  373.  
  374. static PyObject *
  375. r_object(p)
  376.     RFILE *p;
  377. {
  378.     PyObject *v, *v2;
  379.     long i, n;
  380.     int type = r_byte(p);
  381.     
  382.     switch (type) {
  383.     
  384.     case EOF:
  385.         PyErr_SetString(PyExc_EOFError,
  386.                 "EOF read where object expected");
  387.         return NULL;
  388.     
  389.     case TYPE_NULL:
  390.         return NULL;
  391.     
  392.     case TYPE_NONE:
  393.         Py_INCREF(Py_None);
  394.         return Py_None;
  395.     
  396.     case TYPE_ELLIPSIS:
  397.         Py_INCREF(Py_Ellipsis);
  398.         return Py_Ellipsis;
  399.     
  400.     case TYPE_INT:
  401.         return PyInt_FromLong(r_long(p));
  402.     
  403.     case TYPE_INT64:
  404.         return PyInt_FromLong(r_long64(p));
  405.     
  406.     case TYPE_LONG:
  407.         {
  408.             int size;
  409.             PyLongObject *ob;
  410.             n = r_long(p);
  411.             size = n<0 ? -n : n;
  412.             ob = _PyLong_New(size);
  413.             if (ob == NULL)
  414.                 return NULL;
  415.             ob->ob_size = n;
  416.             for (i = 0; i < size; i++)
  417.                 ob->ob_digit[i] = r_short(p);
  418.             return (PyObject *)ob;
  419.         }
  420.     
  421.     case TYPE_FLOAT:
  422.         {
  423.             extern double atof Py_PROTO((const char *));
  424.             char buf[256];
  425.             double dx;
  426.             n = r_byte(p);
  427.             if (r_string(buf, (int)n, p) != n) {
  428.                 PyErr_SetString(PyExc_EOFError,
  429.                     "EOF read where object expected");
  430.                 return NULL;
  431.             }
  432.             buf[n] = '\0';
  433.             PyFPE_START_PROTECT("atof", return 0)
  434.             dx = atof(buf);
  435.             PyFPE_END_PROTECT(dx)
  436.             return PyFloat_FromDouble(dx);
  437.         }
  438.     
  439. #ifndef WITHOUT_COMPLEX
  440.     case TYPE_COMPLEX:
  441.         {
  442.             extern double atof Py_PROTO((const char *));
  443.             char buf[256];
  444.             Py_complex c;
  445.             n = r_byte(p);
  446.             if (r_string(buf, (int)n, p) != n) {
  447.                 PyErr_SetString(PyExc_EOFError,
  448.                     "EOF read where object expected");
  449.                 return NULL;
  450.             }
  451.             buf[n] = '\0';
  452.             PyFPE_START_PROTECT("atof", return 0)
  453.             c.real = atof(buf);
  454.             PyFPE_END_PROTECT(c)
  455.             n = r_byte(p);
  456.             if (r_string(buf, (int)n, p) != n) {
  457.                 PyErr_SetString(PyExc_EOFError,
  458.                     "EOF read where object expected");
  459.                 return NULL;
  460.             }
  461.             buf[n] = '\0';
  462.             PyFPE_START_PROTECT("atof", return 0)
  463.             c.imag = atof(buf);
  464.             PyFPE_END_PROTECT(c)
  465.             return PyComplex_FromCComplex(c);
  466.         }
  467. #endif
  468.     
  469.     case TYPE_STRING:
  470.         n = r_long(p);
  471.         v = PyString_FromStringAndSize((char *)NULL, n);
  472.         if (v != NULL) {
  473.             if (r_string(PyString_AsString(v), (int)n, p) != n) {
  474.                 Py_DECREF(v);
  475.                 v = NULL;
  476.                 PyErr_SetString(PyExc_EOFError,
  477.                     "EOF read where object expected");
  478.             }
  479.         }
  480.         return v;
  481.     
  482.     case TYPE_TUPLE:
  483.         n = r_long(p);
  484.         v = PyTuple_New((int)n);
  485.         if (v == NULL)
  486.             return v;
  487.         for (i = 0; i < n; i++) {
  488.             v2 = r_object(p);
  489.             if ( v2 == NULL ) {
  490.                 Py_DECREF(v);
  491.                 v = NULL;
  492.                 break;
  493.             }
  494.             PyTuple_SET_ITEM(v, (int)i, v2);
  495.         }
  496.         return v;
  497.     
  498.     case TYPE_LIST:
  499.         n = r_long(p);
  500.         v = PyList_New((int)n);
  501.         if (v == NULL)
  502.             return v;
  503.         for (i = 0; i < n; i++) {
  504.             v2 = r_object(p);
  505.             if ( v2 == NULL ) {
  506.                 Py_DECREF(v);
  507.                 v = NULL;
  508.                 break;
  509.             }
  510.             PyList_SetItem(v, (int)i, v2);
  511.         }
  512.         return v;
  513.     
  514.     case TYPE_DICT:
  515.         v = PyDict_New();
  516.         if (v == NULL)
  517.             return NULL;
  518.         for (;;) {
  519.             PyObject *key, *val;
  520.             key = r_object(p);
  521.             if (key == NULL)
  522.                 break; /* XXX Assume TYPE_NULL, not an error */
  523.             val = r_object(p);
  524.             if (val != NULL)
  525.                 PyDict_SetItem(v, key, val);
  526.             Py_DECREF(key);
  527.             Py_XDECREF(val);
  528.         }
  529.         return v;
  530.     
  531.     case TYPE_CODE:
  532.         {
  533.             int argcount = r_short(p);
  534.             int nlocals = r_short(p);
  535.             int stacksize = r_short(p);
  536.             int flags = r_short(p);
  537.             PyObject *code = NULL;
  538.             PyObject *consts = NULL;
  539.             PyObject *names = NULL;
  540.             PyObject *varnames = NULL;
  541.             PyObject *filename = NULL;
  542.             PyObject *name = NULL;
  543.             int firstlineno = 0;
  544.             PyObject *lnotab = NULL;
  545.             
  546.             code = r_object(p);
  547.             if (code) consts = r_object(p);
  548.             if (consts) names = r_object(p);
  549.             if (names) varnames = r_object(p);
  550.             if (varnames) filename = r_object(p);
  551.             if (filename) name = r_object(p);
  552.             if (name) {
  553.                 firstlineno = r_short(p);
  554.                 lnotab = r_object(p);
  555.             }
  556.             
  557.             if (!PyErr_Occurred()) {
  558.                 v = (PyObject *) PyCode_New(
  559.                     argcount, nlocals, stacksize, flags, 
  560.                     code, consts, names, varnames,
  561.                     filename, name, firstlineno, lnotab);
  562.             }
  563.             else
  564.                 v = NULL;
  565.             Py_XDECREF(code);
  566.             Py_XDECREF(consts);
  567.             Py_XDECREF(names);
  568.             Py_XDECREF(varnames);
  569.             Py_XDECREF(filename);
  570.             Py_XDECREF(name);
  571.             Py_XDECREF(lnotab);
  572.  
  573.         }
  574.         return v;
  575.     
  576.     default:
  577.         /* Bogus data got written, which isn't ideal.
  578.            This will let you keep working and recover. */
  579.         Py_INCREF(Py_None);
  580.         return Py_None;
  581.     
  582.     }
  583. }
  584.  
  585. long
  586. PyMarshal_ReadLongFromFile(fp)
  587.     FILE *fp;
  588. {
  589.     RFILE rf;
  590.     rf.fp = fp;
  591.     return r_long(&rf);
  592. }
  593.  
  594. PyObject *
  595. PyMarshal_ReadObjectFromFile(fp)
  596.     FILE *fp;
  597. {
  598.     RFILE rf;
  599.     if (PyErr_Occurred()) {
  600.         fprintf(stderr, "XXX rd_object called with exception set\n");
  601.         return NULL;
  602.     }
  603.     rf.fp = fp;
  604.     return r_object(&rf);
  605. }
  606.  
  607. PyObject *
  608. PyMarshal_ReadObjectFromString(str, len)
  609.     char *str;
  610.     int len;
  611. {
  612.     RFILE rf;
  613.     if (PyErr_Occurred()) {
  614.         fprintf(stderr, "XXX rds_object called with exception set\n");
  615.         return NULL;
  616.     }
  617.     rf.fp = NULL;
  618.     rf.str = NULL;
  619.     rf.ptr = str;
  620.     rf.end = str + len;
  621.     return r_object(&rf);
  622. }
  623.  
  624. PyObject *
  625. PyMarshal_WriteObjectToString(x) /* wrs_object() */
  626.     PyObject *x;
  627. {
  628.     WFILE wf;
  629.     wf.fp = NULL;
  630.     wf.str = PyString_FromStringAndSize((char *)NULL, 50);
  631.     if (wf.str == NULL)
  632.         return NULL;
  633.     wf.ptr = PyString_AS_STRING((PyStringObject *)wf.str);
  634.     wf.end = wf.ptr + PyString_Size(wf.str);
  635.     wf.error = 0;
  636.     w_object(x, &wf);
  637.     if (wf.str != NULL)
  638.         _PyString_Resize(&wf.str,
  639.             (int) (wf.ptr -
  640.                PyString_AS_STRING((PyStringObject *)wf.str)));
  641.     if (wf.error) {
  642.         Py_XDECREF(wf.str);
  643.         PyErr_SetString(PyExc_ValueError, "unmarshallable object");
  644.         return NULL;
  645.     }
  646.     return wf.str;
  647. }
  648.  
  649. /* And an interface for Python programs... */
  650.  
  651. static PyObject *
  652. marshal_dump(self, args)
  653.     PyObject *self;
  654.     PyObject *args;
  655. {
  656.     WFILE wf;
  657.     PyObject *x;
  658.     PyObject *f;
  659.     if (!PyArg_Parse(args, "(OO)", &x, &f))
  660.         return NULL;
  661.     if (!PyFile_Check(f)) {
  662.         PyErr_SetString(PyExc_TypeError,
  663.                 "marshal.dump() 2nd arg must be file");
  664.         return NULL;
  665.     }
  666.     wf.fp = PyFile_AsFile(f);
  667.     wf.str = NULL;
  668.     wf.ptr = wf.end = NULL;
  669.     wf.error = 0;
  670.     w_object(x, &wf);
  671.     if (wf.error) {
  672.         PyErr_SetString(PyExc_ValueError, "unmarshallable object");
  673.         return NULL;
  674.     }
  675.     Py_INCREF(Py_None);
  676.     return Py_None;
  677. }
  678.  
  679. static PyObject *
  680. marshal_load(self, args)
  681.     PyObject *self;
  682.     PyObject *args;
  683. {
  684.     RFILE rf;
  685.     PyObject *f;
  686.     PyObject *v;
  687.     if (!PyArg_Parse(args, "O", &f))
  688.         return NULL;
  689.     if (!PyFile_Check(f)) {
  690.         PyErr_SetString(PyExc_TypeError,
  691.                 "marshal.load() arg must be file");
  692.         return NULL;
  693.     }
  694.     rf.fp = PyFile_AsFile(f);
  695.     rf.str = NULL;
  696.     rf.ptr = rf.end = NULL;
  697.     PyErr_Clear();
  698.     v = r_object(&rf);
  699.     if (PyErr_Occurred()) {
  700.         Py_XDECREF(v);
  701.         v = NULL;
  702.     }
  703.     return v;
  704. }
  705.  
  706. static PyObject *
  707. marshal_dumps(self, args)
  708.     PyObject *self;
  709.     PyObject *args;
  710. {
  711.     PyObject *x;
  712.     if (!PyArg_Parse(args, "O", &x))
  713.         return NULL;
  714.     return PyMarshal_WriteObjectToString(x);
  715. }
  716.  
  717. static PyObject *
  718. marshal_loads(self, args)
  719.     PyObject *self;
  720.     PyObject *args;
  721. {
  722.     RFILE rf;
  723.     PyObject *v;
  724.     char *s;
  725.     int n;
  726.     if (!PyArg_Parse(args, "s#", &s, &n))
  727.         return NULL;
  728.     rf.fp = NULL;
  729.     rf.str = args;
  730.     rf.ptr = s;
  731.     rf.end = s + n;
  732.     PyErr_Clear();
  733.     v = r_object(&rf);
  734.     if (PyErr_Occurred()) {
  735.         Py_XDECREF(v);
  736.         v = NULL;
  737.     }
  738.     return v;
  739. }
  740.  
  741. static PyMethodDef marshal_methods[] = {
  742.     {"dump",    marshal_dump},
  743.     {"load",    marshal_load},
  744.     {"dumps",    marshal_dumps},
  745.     {"loads",    marshal_loads},
  746.     {NULL,        NULL}        /* sentinel */
  747. };
  748.  
  749. void
  750. PyMarshal_Init()
  751. {
  752.     (void) Py_InitModule("marshal", marshal_methods);
  753. }
  754.